// pages/address/list/list.js
import { findAddressList, deleteAddressById } from '../../../utils/api'
Page({
  /**
   * 页面的初始数据
   */
  data: {
    addressList: [], //地址列表数据
  },
  async getAddressList() {
    try {
      const result = await findAddressList()
      // console.log(result)
      if (result.code === 200) {
        this.setData({
          addressList: result.data
        })
      }
    } catch (error) {
      console.log(error)
    }
  },
  async deleteAddress(id){
    try {
      const result=await deleteAddressById(id)
      // console.log(result);
      result.code===200&&this.getAddressList()
    } catch (error) {
      console.log(error);
    }
  },
  //删除地址的事件对应的回调函数
  deleteAddressHandler(event) {
    const { id, address } = event.currentTarget.dataset
    wx.showModal({
      title: '您确定要删除吗？',
      content:address,
      showCancel: true,
      cancelText: '取消',
      cancelColor: '#000000',
      confirmText: '确定',
      confirmColor: '#3CC51F',
      success: (res) => {
        res.confirm&&this.deleteAddress(id)
      },
      
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getAddressList()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
})
